package com.shen.bq;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

public class Test {
    public static void main(String[] args) {
        test4();
    }

    /**
     * 抛出异常
     */
    public static void test1(){



        //队列的大小  设置为3
        ArrayBlockingQueue arrayBlockingQueue=new ArrayBlockingQueue<>(3);

        System.out.println("====================================");

        /**
         * 添加
         */
        System.out.println(arrayBlockingQueue.add("a"));//true
        System.out.println(arrayBlockingQueue.add("b"));//true
        System.out.println(arrayBlockingQueue.add("c"));//true

        //添加第四个d
       // arrayBlockingQueue.add("d");
        //Exception in thread "main" java.lang.IllegalStateException: Queue full  队列已满

        System.out.println("====================================");

        /**
         * 弹出队首的元素
         */

        System.out.println(arrayBlockingQueue.element()); //a



        System.out.println("====================================");

        /**
         * 弹出
         */

        //remove是按照FIFO（先进先出）顺序弹出，所以没有参数
        System.out.println(arrayBlockingQueue.remove());//a
        System.out.println(arrayBlockingQueue.remove());//b
        System.out.println(arrayBlockingQueue.remove());//c

        System.out.println(arrayBlockingQueue.remove());//删除第四个元素
        //Exception in thread "main" java.util.NoSuchElementException  没有该元素问题



    }

    /**
     * 不抛出异常 有返回值
     */
    public static void test2(){
        ArrayBlockingQueue arrayBlockingQueue=new ArrayBlockingQueue(3);

        System.out.println("=====================================");

        /**
         * 添加
         */
        System.out.println(arrayBlockingQueue.offer("a"));//true
        System.out.println(arrayBlockingQueue.offer("b"));//true
        System.out.println(arrayBlockingQueue.offer("c"));//true
        System.out.println(arrayBlockingQueue.offer("d"));//false

        System.out.println("=====================================");
        /**
         * 弹出队首
         */

        System.out.println(arrayBlockingQueue.peek());//a

        System.out.println("=====================================");
        /**
         * 弹出
         */
        System.out.println(arrayBlockingQueue.poll());//a
        System.out.println(arrayBlockingQueue.poll());//b
        System.out.println(arrayBlockingQueue.poll());//c
        System.out.println(arrayBlockingQueue.poll());//null

    }

    /**
     * 等待，阻塞（一直阻塞）
     */
    public static void test3(){
        ArrayBlockingQueue arrayBlockingQueue=new ArrayBlockingQueue(3);

        try {
            System.out.println("=====================================");
            /**
             * 添加
             */

            arrayBlockingQueue.put("a");
            arrayBlockingQueue.put("b");
            arrayBlockingQueue.put("c");
            //arrayBlockingQueue.put("d"); //队列中没有第四个位置了，所以d就会一直等待

            System.out.println("=====================================");

            /**
             * 拿取
             */

            System.out.println(arrayBlockingQueue.take());//a
            System.out.println(arrayBlockingQueue.take());//b
            System.out.println(arrayBlockingQueue.take());//c
            //当拿取第四个元素时，队列中是没有第四个元素的，这个take()就会一直等待



        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    /**
     * 等待，阻塞（等待超时）
     */

    public static void test4(){
        ArrayBlockingQueue arrayBlockingQueue=new ArrayBlockingQueue(3);

        System.out.println("=====================================");

        /**
         * 添加
         */

        System.out.println(arrayBlockingQueue.offer("a"));//true
        System.out.println(arrayBlockingQueue.offer("b"));//true
        System.out.println(arrayBlockingQueue.offer("c"));//true
        try {
            //当超时2秒后还是不能加入队列就放弃阻塞   off("要加入的元素",秒数,单位TimeUntil)
            System.out.println(arrayBlockingQueue.offer("d", 2, TimeUnit.SECONDS));//无返回
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("=====================================");
        /**
         * 删除
         */
        System.out.println(arrayBlockingQueue.poll());//a
        System.out.println(arrayBlockingQueue.poll());//b
        System.out.println(arrayBlockingQueue.poll());//c
        try {
            //当超时2秒后还是不能取出元素就放弃阻塞   poll(秒数,单位TimeUntil)
            System.out.println(arrayBlockingQueue.poll(2,TimeUnit.SECONDS));//null
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
